package base.day08;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class study01 {
    public static void main(String[] args) {
        JFrame jf = new JFrame();
        jf.setTitle("用户登录");
        jf.setSize(400, 300);
        jf.setDefaultCloseOperation(3);
        jf.setLocationRelativeTo(null);
        jf.setAlwaysOnTop(true);
        jf.setLayout(null);

        //用户名标题
        JLabel username = new JLabel("用户名");
        username.setBounds(50, 50, 50, 20);
        jf.add(username);
        //用户名文本框
        JTextField usernamefield = new JTextField();
        usernamefield.setBounds(150, 50, 180, 20);
        jf.add(usernamefield);
        //密码标题
        JLabel password = new JLabel("密码");
        password.setBounds(50, 100, 50, 20);
        jf.add(password);
        //密码文本框
        JTextField passwordfield = new JTextField();
        passwordfield.setBounds(150, 100, 180, 20);
        jf.add(passwordfield);

        JButton button = new JButton("登录");
        button.setBounds(50, 200, 280, 20);
        jf.add(button);

        //存储的用户名和密码
        String name = "zhangchao";
        String pass = "123456";
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String user = usernamefield.getText();
                String password1 = passwordfield.getText();
                if (user.length() > 9 || user.length() < 4) {
//                    System.out.println("请输入1-9位的字符");
                    JOptionPane.showMessageDialog(jf, "请输入1-9位的字符");
                    usernamefield.setText("");
                    return;
                }
                if (user.equals(name) && password1.equals(pass)) {
//                    System.out.println("登陆成功");
                    JOptionPane.showMessageDialog(jf, "登陆成功");
                    usernamefield.setText("");
                    passwordfield.setText("");

                } else {
//                    System.out.println("登陆失败，用户名或密码错误");
                    JOptionPane.showMessageDialog(jf, "登陆失败，用户名或密码错误");
                    usernamefield.setText("");
                    passwordfield.setText("");
                }
            }
        });

        jf.setVisible(true);

    }
}
